import json
import random

from flask import Blueprint
from RootPath import RootPath
import os
from common.YmlUtil import YmlUtil
from common.contactStr import sqlContact, mqttMessageContact
from common.mqttUtil import on_mqtt_connect, publishMqttData
from common.mysqlUtil import exc_sql
generPr = Blueprint("deviceGenerator", __name__)
path = os.path.join(RootPath.getRootPath(), "Application.yml")
MQTTconf = YmlUtil().readYml(path,"mqtt_test_131")
MySQLconf = YmlUtil().readYml(path,"mysql_test_131")
deviceID="9009"

class ergongAlarm():
    def exc(self):
        print(" ======ergong======")
        point_value={"No1FrequencyCurrent":"44","No1FrequencyOutput":json.dumps(random.randint(40,55)),
                     "No1FrequencyTemp":json.dumps(random.randint(20,25)),"InletPressure":json.dumps(random.randint(60,100)),
                     "PhaseInversionAlarm":"1"}
        sqldata=sqlContact(deviceID,point_value)
        mqttdata=json.dumps(mqttMessageContact(deviceID,point_value))
        print(sqldata)
        print(mqttdata)
        on_mqtt_connect(MQTTconf)
        publishMqttData(MQTTconf,mqttdata)#推送mqtt消息要用json dump方法
        exc_sql(sqldata,MySQLconf)


# ergongAlarm().exc()